<template>
  <div>
    <vxe-table :data="formPerms" border align="center">
      <vxe-table-column title="表单字段" field="title" align="left" header-align="center">
      </vxe-table-column>
      <vxe-table-column field="readOnly" title="只读" width="80">
        <template #header>
          <vxe-button status="primary"  round @click="allSelect('R')">只读</vxe-button>
        </template>
        <template #default="{ row }">
          <vxe-radio v-model="row.perm" label="R" :name="row.id"></vxe-radio>
        </template>
      </vxe-table-column>
      <vxe-table-column field="editable" title="可编辑" width="90" v-if="nowNode.type !== 'CC'">
        <template #header>
          <vxe-button status="primary"  round @click="allSelect('E')">可编辑</vxe-button>
        </template>
        <template #default="{ row }">
          <vxe-radio v-model="row.perm" label="E" :name="row.id"></vxe-radio>
        </template>
      </vxe-table-column>
      <vxe-table-column field="hide" title="隐藏" width="80">
        <template #header>
          <vxe-button status="primary"  round @click="allSelect('H')">隐藏</vxe-button>
        </template>
        <template #default="{ row }">
          <vxe-radio v-model="row.perm" label="H" :name="row.id"></vxe-radio>
        </template>
      </vxe-table-column>
    </vxe-table>
  </div>
</template>

<script>
export default {
  name: "FormAuthorityConfig",
  components: {},
  data() {
    return {
      permSelect: '',
    }
  },

  created() {
    // this.init()
  },
  computed: {
    nowNode() {
      this.init()
      return this.$store.state.flow.selectedNode
    },
    formData() {
      return this.$store.state.flow.design.formItems
    },
    formPerms() {
      return this.$store.state.flow.selectedNode.props.formPerms
    }
  },
  methods: {
    init() {
      //备份
      let oldPermMap = this.formPerms.toMap('id')
      //重新清空，按顺序加载权限
      this.formPerms.length = 0;
      this.formPermsLoad(oldPermMap, this.formData)
    },
    allSelect(type) {
      this.permSelect = type
      this.formPerms.forEach(f => f.perm = type)
    },
    formPermsLoad(oldPermMap, forms) {
      forms.forEach(form => {
        if (form.name === 'SpanLayout') {
          this.formPermsLoad(oldPermMap, form.props.items)
        } else {
          //刷新名称
          let old = oldPermMap.get(form.key)
          if (old) {
            old.title = form.label
            // old.required = form.props.required
            this.formPerms.push(old)
          } else {
            this.formPerms.push({
              id: form.key,
              title: form.label,
              // required: form.props.required,
              perm: this.$store.state.flow.selectedNode.type === 'ROOT' ? 'E' : 'R'
            })
          }
        }
      })
    },
    handleCheckAllChange() {

    }
  },
  watch: {
    formPerms: {
      deep: true,
      handler() {
        const set = new Set(this.formPerms.map(f => f.perm))
        this.permSelect = set.size === 1 ? set.values()[0] : ''
      }
    },

  }
}
</script>

<style lang="scss" scoped>

:deep .el-table__row {
  & > td:first-child {
    .cell {
      text-align: left;
    }
  }

  .cell {
    text-align: center;
  }

  .el-radio__label {
    display: none;
  }
}
</style>
